package ma.ensao.egicom.dao.implementations;

import java.util.List;

import ma.ensao.egicom.core.model.Product;
import ma.ensao.egicom.dao.interfaces.ProductDAO;

import org.hibernate.Query;
import org.scub.foundation.framework.core.dao.implementations.HibernateDaoBaseImplementation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Product entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see pack.Product
 * @author MyEclipse Persistence Tools
 */
public class ProductDAOImpl extends HibernateDaoBaseImplementation implements ProductDAO{
	private static final Logger log = LoggerFactory.getLogger(ProductDAO.class);
	// property constants
	public static final String ID = "id";
	public static final String LABEL = "label";
	public static final String DESCRIPTION = "description";
	public static final String IMAGE = "image";
	public static final String PRICE = "price";
	public static final String CATEGORYID = "categoryid";
	
	/**
     * {@inheritDoc}
     */
    @Override
	public void save(Product transientInstance) {
		save(transientInstance);
	}

    /**
     * {@inheritDoc}
     */
    @Override
	public void delete(Product persistentInstance) {
		delete(persistentInstance);
			
	}

    /**
     * {@inheritDoc}
     */
    @Override
	public Product findById(java.lang.Integer id) {
        return load(id, Product.class);
    }

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Product instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Product as model where model."
					+ propertyName + "= ?";
			Query queryObject = createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findById(Object id) {
		return findByProperty(ID, id);
	}

	public List findByLabel(Object label) {
		return findByProperty(LABEL, label);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByCategory(int category){
	    return findByProperty(CATEGORYID, category);
	}
	 
	public List<Product> findAll() {
	    return loadAll(Product.class);
	}

}